import { get, post, put, del } from '@/utils/request'
import { Role, Permission, User, PaginationResponse, ApiResponse } from '@/types'

// 角色管理API
export const roleApi = {
  // 获取角色列表
  getList: (params: { page: number; pageSize: number; name?: string }) => {
    return get<PaginationResponse<Role>>('/roles', params)
  },
  
  // 获取所有角色（不分页）
  getAll: () => {
    return get<Role[]>('/roles/all')
  },
  
  // 获取角色详情
  getDetail: (id: string) => {
    return get<Role>(`/roles/${id}`)
  },
  
  // 创建角色
  create: (data: Partial<Role>) => {
    return post<Role>('/roles', data)
  },
  
  // 更新角色
  update: (id: string, data: Partial<Role>) => {
    return put<Role>(`/roles/${id}`, data)
  },
  
  // 删除角色
  delete: (id: string) => {
    return del<ApiResponse>(`/roles/${id}`)
  },
  
  // 获取角色用户
  getUsers: (id: string, params: { page: number; pageSize: number }) => {
    return get<PaginationResponse<User>>(`/roles/${id}/users`, params)
  },
  
  // 分配用户到角色
  assignUsers: (id: string, userIds: string[]) => {
    return post<ApiResponse>(`/roles/${id}/users`, { userIds })
  },
  
  // 移除角色用户
  removeUser: (id: string, userId: string) => {
    return del<ApiResponse>(`/roles/${id}/users/${userId}`)
  },
  
  // 更新角色权限
  updatePermissions: (id: string, permissionIds: string[]) => {
    return put<Role>(`/roles/${id}/permissions`, { permissionIds })
  }
}

// 权限管理API
export const permissionApi = {
  // 获取权限列表
  getList: (params: { type?: string; status?: string }) => {
    return get<Permission[]>('/permission/permissions', params)
  },
  
  // 获取权限树
  getTree: () => {
    return get<Permission[]>('/permission/permissions/tree')
  },
  
  // 获取权限详情
  getDetail: (id: string) => {
    return get<Permission>(`/permission/permissions/${id}`)
  },
  
  // 创建权限
  create: (data: Partial<Permission>) => {
    return post<Permission>('/permission/permissions', data)
  },
  
  // 更新权限
  update: (id: string, data: Partial<Permission>) => {
    return put<Permission>(`/permission/permissions/${id}`, data)
  },
  
  // 删除权限
  delete: (id: string) => {
    return del<ApiResponse>(`/permission/permissions/${id}`)
  },
  
  // 更新权限状态
  updateStatus: (id: string, status: 'active' | 'inactive') => {
    return put<Permission>(`/permission/permissions/${id}/status`, { status })
  }
} 